<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Function learning</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script>
        'use strict'
        //取绝对值
        function abs(x) {
            console.log('-------------abs-----------------');
            if (typeof x !== 'number') {
                //不是数字的话就报错Not a Number
                throw 'Not a number';
            }
            if (x >= 0) {
                //是正数直接返回
                return x;
            } else {
                //负负得正返回
                return -x;
            }
        }
        //arg(10,20,30)
        function arg(x) {
            console.log('------------arguments----------');
            //x只能拿到10
            console.log('x=' + x);
            for (let i = 0; i < arguments.length; i++) {
                //arguments可以拿到所有参数
                console.log('arg' + i + '=' + arguments[i]);
            }

        }

        // foo(a,[ b], c)
        // 接收2~3个参数，b是可选参数，如果只传2个参数，b默认为null：
        function foo(a, b, c) {
            console.log('------------foo----------');
            if (arguments.length === 3) {
                // 实际拿到的参数是a和b，c为undefined
                c = b; // 把b赋给c
                b = null; // b变为默认值

            }
            console.log('a=' + arguments[0]);
            console.log('b=' + arguments[1]);
            console.log('c=' + arguments[2]);
        }

        //      a  b
        //arg2(10,20,30,40,50,60,50)
        //获取a，b之外的参数，放到rest数组里面
        function foo2(a, b) {
            console.log('-----------foo2-----------')

            let i, rest = [];
            if (arguments.length > 2) {
                for (i = 2; i < arguments.length; i++) {
                    rest.push(arguments[i]);
                }
            }
            console.log('a =' + a);
            console.log('b =' + b);
            console.log(rest);
        }

        //获取a，b之外的参数，放到rest数组里面
        function foo3(a, b, ...rest) {
            console.log('-----------foo3-----------')
            console.log('a =' + a);
            console.log('b =' + b);
            console.log(rest);
        }

        //用rest接收任意个参数并返回他们的和
        function sum(...rest) {
            console.log('-----------sum-----------')
            let sum = 0;
            for (let i = 0; i < rest.length; i++) {
                sum += rest[i];
            }
            console.log(sum);
        }

        //输入r和pi求面积，如果没给pi值，就按3.14计算
        function area_of_circle(r, pi) {
            console.log('-----------area-----------')
            let sum;
            if (pi == "" || pi ==null) {
                pi = 3.14;
                sum = pi * r * r;
                console.log('sum1:' + sum);
            } else {
                sum = pi * r * r;
                console.log('sum2:' + sum);
            }
        }


        let test1 = abs(23);
        console.log(test1);
        let test2 = arg(10, 20, 30);
        console.log(test2);

        foo(10, [], 30);
        foo2(10, 20, 30, 40, 50, 60, 50);
        foo3(1, 2, 3, 4, 5, 6);
        sum(54, 66, 88, 77, 66, 99);
        area_of_circle(3);
    </script>
</head>

<body>

</body>

</html>